//
//  YIem_SideslipViewController.m
//  B段_开眼
//
//  Created by YIem on 16/4/25.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_SideslipViewController.h"
#import "YIem_SYDockView.h"
#import "YIem_UIView+SYCategory.h"
#import "YIem_SY_Four_ViewController.h"

#import "YIem_SY_One_ViewController.h"


const NSString * SYTabBarSelectIndex = @"SYTabBarSelectIndex";
@interface YIem_SideslipViewController ()<UINavigationBarDelegate>




//添加测栏
@property (nonatomic,strong)YIem_SYDockView *dockView;
@property (nonatomic, strong) UIScrollView *scroll;

@property (nonatomic,strong)UIViewController *selectVC;
@end

@implementation YIem_SideslipViewController
//懒加载
- (YIem_SYDockView *)dockView
{
    if (_dockView == nil) {
        
        // 创建
        YIem_SYDockView *dockView = [[YIem_SYDockView  alloc] init];
        [self.view addSubview:dockView];
        
//        self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//        self.scroll.contentSize = CGSizeMake(0, self.view.frame.size.height);
//        self.scroll.bounces = NO;
//        self.scroll.showsHorizontalScrollIndicator = NO;
//        [_scroll addSubview:dockView];
//        
//        [_dockView addSubview:_scroll];
        
        _dockView = dockView;
    }
    return _dockView;
}
- (void)viewDidLoad {
    [super viewDidLoad];


//    self.navigationItem.title = @"11";
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"YIem";
    
    UIScreenEdgePanGestureRecognizer *screenGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screenGR.edges = UIRectEdgeRight;
    [self.view addGestureRecognizer:screenGR];
    
    
    // 返回
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal2"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.2077 green:0.2077 blue:0.2077 alpha:1.0];
    

      //监听通知]
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarButtonChangeNotification:) name:@"SYTabBarButtonChangeNotification" object:nil];
    
    //创建控制器
      [self setupViewControllers];
    
    
    //ios8之后的写法
    [self viewWillTransitionToSize:[UIScreen mainScreen].bounds.size withTransitionCoordinator:self.transitionCoordinator];
    
 
    
}
// 通知
- (void)tabBarButtonChangeNotification:(NSNotification *)notification {
   NSInteger index = [notification.userInfo[SYTabBarSelectIndex]integerValue];
    //切换控制器
    [self changeViewController:index];
}
//dealloc，用来移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//创建控制器
-(void)setupViewControllers{
    
    //创建子控制器
    for (int i=0; i<6; i++) {
        UIViewController *viewVC = [[UIViewController alloc]init];
        viewVC.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
        viewVC.view.frame = CGRectMake(210, 0, 500, 700);
        [self addChildViewController:viewVC];
    }
    //直接显示第一控制器
    [self changeViewController:0];
    
}
-(void)changeViewController:(NSInteger)index{
    
    NSLog(@"切换控制器%ld", index);
    //通过切换索引来切换控制器
    //先移除旧的视图
    [self.selectVC.view removeFromSuperview];

    
    if (index == 0) {
        [self.selectVC.view removeFromSuperview];
        
        YIem_SY_One_ViewController *newVC = [[YIem_SY_One_ViewController alloc] init];
        //属性更换成新的视图
        self.selectVC = newVC;
        
        //添加新的view到主视图
        [self.view addSubview:newVC.view];
        
        //设置约束
        [newVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self.view);
           make.top.bottom.equalTo(self.view).top.offset(64);
            make.left.equalTo(self.dockView.mas_right);
            

//            make.left.bottom.equalTo(self.view);
        }];
        
    }else if (index == 2){
        
    }else if (index == 3){
        
        [self.selectVC.view removeFromSuperview];
        
        YIem_SY_Four_ViewController *fourVC = [[YIem_SY_Four_ViewController alloc] init];
//        fourVC.view.backgroundColor = [UIColor greenColor];
        //属性更换成新的视图
        self.selectVC = fourVC;
        
        //添加新的view到主视图
        [self.view addSubview:fourVC.view];
        
        //设置约束
        [fourVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.equalTo(self.view);
            make.top.bottom.equalTo(self.view).top.offset(64);
            make.left.equalTo(self.dockView.mas_right);
            
            
            //            make.left.bottom.equalTo(self.view);
        }];

        
        
    }else {
        
   [self.selectVC.view removeFromSuperview];
    //获取新的
    UIViewController *newVC = self.childViewControllers[index];
    
    //属性更换成新的视图
    self.selectVC = newVC;
    
    //添加新的view到主视图
    [self.view addSubview:newVC.view];
    
    //设置约束
    [newVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.bottom.equalTo(self.view);
//        make.left.equalTo(self.dockView.mas_right);
        make.right.bottom.equalTo(self.view);
        make.top.bottom.equalTo(self.view).top.offset(64);
        make.left.equalTo(self.dockView.mas_right);
    }];
        
    }
}
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    

        
        NSLog(@"竖屏");
    
        self.dockView.x = 0;
        self.dockView.y = 64;
        self.dockView.width = 70;
        self.dockView.height = self.view.frame.size.height - 64;
  
}




// 返回
- (void)rightAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)sender
{
    // 防止一次操作 多次进入
    if (UIGestureRecognizerStateEnded == sender.state) {
//        NSLog(@"出来");
        
        
        [UIView animateWithDuration:1.0 animations:^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
